﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AcademicPlanner.Data
{
	/// <summary>
	/// List of application level error codes in the data layer of the
	/// AcademicPlanner application.
	/// </summary>
	/// <version>1.0</version>
	/// <since>1.0</since>
	public enum DataErrorCode
	{
		NONE,
		SAVE_ENTITY_FAILED,
		GET_ENTITY_FAILED,
		GET_NAMED_QUERY_FAILED,
		QUERY_PERSON_ENTITY_BY_EMAIL_FAILED,
		QUERY_ACOBJECT_TOKEN_ENTITY_BY_OBJECT_AND_PURPOSE_FAILED,
		QUERY_ACCOUNT_ENTITY_BY_USER_USERNAME_FAILED
	}

	/// <summary>
	/// DataException represents an application level exception in the
	/// data layer of the AcademicPlanner application.
	/// </summary>
	/// <version>1.0</version>
	/// <since>1.0</since>
	public class DataException : ApplicationException
	{
		DataErrorCode errorCode;


		/// <summary>
		/// The error code associated with this DataException.
		/// </summary>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public DataErrorCode ErrorCode { get { return errorCode; } }

		/// <summary>
		/// Gets the error message associated with this DataException.
		/// </summary>
		public override string Message { get { return GetErrorCodeMessage(errorCode) + " - " + InnerException.Message; } }


		/// <summary>
		/// Constructs a DataException with the given <c>DataErrorCode</c> and
		/// <c>InnerException</c>.
		/// </summary>
		/// <param name="pCode">The <c>DataErrorCode</c>.</param>
		/// <param name="pInnerException">The <c>InnerException</c>.</param>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public DataException(DataErrorCode pCode, Exception pInnerException) : base("", pInnerException)
		{
			errorCode = pCode;
		}

		/// <summary>
		/// Gets the error message associated with a given <c>DataErrorCode</c>.
		/// </summary>
		/// <param name="pCode">The <c>DataErrorCode</c>.</param>
		/// <returns>The error message.</returns>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public static string GetErrorCodeMessage(DataErrorCode pCode)
		{
			// TODO: RETURN MORE COMPREHENSIBLE MESSAGE
			return pCode.ToString();
		}
	}
}
